/* *
	*
	* made by ilfate.=/[-7
	* started at 27.05.11
	*
	*/
	
	
function Pulsar(canvas,options)
{
  this.options = options;
  if(!this.options['interval']) this.options['interval'] = 10;
  if(!this.options['time_limit']) this.options['time_limit'] = 30;
  this.events = [];
  this.layers = [];
  this.func = [];
  this.time_func = [];
  this.ready = 0;
  this.start_on_ready = 0;
  this.id_interval = 0;
  this.time = 0;
  this.runing = 0;
  this.shutDown = false;
  
  	
  this.div = document.getElementById(canvas);
  for( var attr in this.options)
  {
    if(attr != 'interval')
      this.div[attr] = this.options[attr];
  }
  if(this.options['fps'])
  {
    var d = new Date();
    var ms = d.getMilliseconds();
    this.fps_data = {'sec':0,'count':0,'last':ms};
    this.fps = $('<div>').attr('id','fps');
    $(this.div).after(this.fps);
    
  }
	
	
  this.setOptions = function(options)
  {
    this.options = options;
  }
	
  this.addLayer = function(name,index)
  {
    if(this.layers[index] != undefined) 
    {
      console.error('Error layer creating');
      return false;
    }
    else
    {
      this.layers[index] = new Layer(name,index,this);
    }
				
    var canvas_elem = document.createElement('canvas');
    canvas_elem.id = name;
    canvas_elem.className = "Pulsar_Canvas";
    canvas_elem.style.zIndex = index;
    this.div.appendChild(canvas_elem);
    $(canvas_elem).css('position','absolute');
    for( var attr in this.options)
    {
      if(attr != 'interval')
        canvas_elem[attr] = this.options[attr];
    }
    this.layers[index].ctx = canvas_elem.getContext('2d');
    this.layers[index].tag = canvas_elem;
		
    return this.layers[index];
  }

  this.getLayer = function(name)
  {
    for( var key in this.layers )
    {
      if(this.layers[key].name == name)
      {
        return this.layers[key];
      }
    }
    return false;
  }
  /*
   * try to start draw.
   */
  this.tryReady = function()
  {//console.info(1);
    for( var key in this.layers )
    {
      if(this.layers[key].ready != 1)
      {
        this.ready = 0;
        return false;
      }
    }
    this.ready = 1;
    if(this.start_on_ready == 1)
    {
      this.draw();
    }
    return this;
  }
	
  this.draw = function() 
  {
    if(this.ready == 1)
    {
      if(this.fps )
      {
        var d = new Date();
        var sec = d.getSeconds();
        var ms = d.getMilliseconds();
        
        dms = ms - this.fps_data.last;
        if(dms<0) {dms = 1000 - this.fps_data.last + ms;
        }
        this.fps_data.last = ms;
        var fps_2 = parseInt(1000 / dms);
        if(this.fps_data.sec == sec){
          this.fps_data.count++;
          var fps_1 = this.fps_data.count_last;
        }else{
          var fps_1 = this.fps_data.count;
          this.fps_data.count = 1;
          this.fps_data.count_last = fps_1;
          this.fps_data.sec = sec;
        }
        this.fps.html('FPS: '+fps_1 + ' | '+fps_2);
      }
      for( var key in this.layers )
      {
				//console.info(this.layers[key].name +' key='+ key);
        this.layers[key].draw();
      }
			
      if(this.runing == 0)
        this.run(this);
      
    }
    else
    {
      this.start_on_ready = 1;
    }
  }


  this.run = function(pulsar)
  {
    this.runing = 1;
    //this.id_interval = setInterval(function(){pulsar.update();},this.options['interval'])
    this.id_interval = requestInterval(function(){pulsar.update();}, this.options['interval']);
  }

  this.addFunction = function(f)
  {
    this.func[this.func.length+1] = f;
    return this;
  }

  this.addTimeFunction = function(f,time)
  {
    this.time_func[time] = f;
    return this;
  }

  this.clearAll = function()
  {
		//	this.ctx.clearRect(0,0,this.options['width'],this.options['height']);		
  }

  this.update = function(time)
  {
    this.time += this.options['interval'];
    if(this.time > this.options['time_limit']*1000 || this.shutDown) {
      clearRequestInterval(this.id_interval);
      console.info('end');
    }
    
    for(var key in this.func)
    {
      this.func[key](this);
    }
    for(var key in this.time_func)
    {
      if(key <= this.time)
      {
        this.time_func[key](this);
        delete this.time_func[key];
      }
    }
    //*/
    this.clearAll();
    this.draw();
  }
	
	
}


/**
* Behaves the same as setInterval except uses requestAnimationFrame() where possible for better performance
* @param {function} fn The callback function
* @param {int} delay The delay in milliseconds
*/
window.requestInterval = function(fn, delay) {
if( !window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) && // Firefox 5 ships without cancel support
!window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame)
return window.setInterval(fn, delay);

var start = new Date().getTime(),
handle = new Object();

function loop() {
var current = new Date().getTime(),
delta = current - start;

if(delta >= delay) {
fn.call();
start = new Date().getTime();
}

handle.value = requestAnimFrame(loop);
};

handle.value = requestAnimFrame(loop);
return handle;
}

requestAnimFrame = (function() {
  return window.requestAnimationFrame ||
     window.webkitRequestAnimationFrame ||
     window.mozRequestAnimationFrame ||
     window.oRequestAnimationFrame ||
     window.msRequestAnimationFrame ||
     function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
       window.setTimeout(callback, 1000/60);
     };
})();


/**
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance
* @param {int|object} fn The callback function
*/
    window.clearRequestInterval = function(handle) {
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
    window.webkitCancelAnimationFrame ? window.webkitCancelAnimationFrame(handle.value) :
    window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */
    window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
    window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) :
    window.msCancelRequestAnimationFrame ? window.msCancelRequestAnimationFrame(handle.value) :
    clearInterval(handle);
};

/*

function extend(Child, Parent) {
	var F = function() { }
	F.prototype = Parent.prototype
	Child.prototype = new F()
	Child.prototype.constructor = Child
	Child.superclass = Parent.prototype
}
//*/
